<?php

function setAccessTags($tagIds, $fromUserName, $module, $moduleId){
		
	if (empty($tagIds)) {
		return;
	}
	
	$tagIds=split(',', $tagIds);
	foreach($tagIds as $key=>$value){
		setAccessTag($value, $fromUserName, $module, $moduleId);
	}
	return;
}

	function setAccessTagCount($accesstagid, $fromUserName){
			$accessTagCount = M('user_access_tag_count');			
			$where['accesstagid']=$accesstagid;
			$where['fromUserName']=$fromUserName;
			//判断标签表是否存在标签 若不存在则新建标签
			$res=$accessTagCount->where($where)->find();
		    if(!isset($res)){
				    $accessTagCount ->accesstagid =$accesstagid;
					$accessTagCount ->fromUserName =$fromUserName;
				    $accessTagCount ->count =1;
				    $result = $accessTagCount->add();
		    }else{ 
		    		$data["count"] = $res["count"]+1;
		    		$result = $accessTagCount->where($where)->save($data);
		    		if(!$result){
		    			log::write("setAccessTagCount add error");
		    		}
		    }
		  return;
	}
	function setAccessTag($tagId, $fromUserName, $module, $moduleId){
			$accessTagModel = M('access_tag');
			$userAccessTagModel = M('user_access_tag');
			$where['id']=$tagId;
			//判断标签表是否存在标签 若不存在则新建标签
			$res=$accessTagModel->where($where)->find();
			$accesstagid=$res['id'];
			if(!$res) {
				return;
			}
			// if(!isset($res)){
			// 	$accesstagid=$accessTagModel->add($where);		
			// }
			$userWhere['fromUserName']=$fromUserName;
			$userWhere['accesstagid']=$res['id'];
			$userRes=$userAccessTagModel->where($userWhere)->find();
			Log::write("添加访问标签".$userAccessTagModel->getlastsql());
			if(!isset($userRes)){
				$userWhere['module']=$module;	
				$userWhere['moduleId']=$moduleId;
				$userWhere['createTime']= date('Y-m-d H:i:s',time());				
				$userWhere['tagName']=$res['tagName'];	
				$result=$userAccessTagModel->add($userWhere);
				if(!$result){
				log::write("setAccessTag add error");
				}
			}
			setAccessTagCount($res['id'], $fromUserName);
			  return;
	}



	function delUserAccessTag($tagName, $fromUserName){
			$userAccessTagModel = M('user_access_tag');
			$where['tagName']=$tagName;
			$where['fromUserName']=$fromUserName;
			$res=$userAccessTagModel->where($where)->delete();
			  return;
	}

	
	 function addAccessTags($accessTags){
			$accessTagModel = M('access_tag');
			$accessTags=split(',', $accessTags);
				foreach($accessTags as $key=>$value){				
						$where['tagName']=$value;
						//判断标签表是否存在标签 若不存在则新建标签
						$res=$accessTagModel->where($where)->find();	
						if(!isset($res)){
							$result=$accessTagModel->add($where);
						}
				}
	 		    if(!$result){
		    		log::write("addAccessTags add error");
		    	}
		  return;
		}	
		




//添加标签
/*
function set_accesstag($tagName, $fromUserName, $module, $moduleId) {
	echo 'asdfadsf';
	exit;
	$accessTagModel = M('access_tag');
	$userAccessTagModel = M('user_access_tag');
	$where['tagName']=$tagName;
	//判断标签表是否存在标签 若不存在则新建标签
	$res=$accessTagModel->where($where)->find();
	$accesstagid=$res[id];
	if(!isset($res)){
		//$data['tagName']=$tagName;
		$accesstagid=$accessTagModel->add($where);
	}
	
	$userWhere['fromUserName']=$fromUserName;
	$userRes=$userAccessTagModel->where($userWhere)->find();
		//判断用户是否设置了标签
		$userResarray=explode(',',$userRes['accesstagid']);
		foreach($userResarray as $value){
			if($accesstagid == $value){
			return false;
			}
		
		}
		$accesstagid=$userRes['accesstagid'].','.$accesstagid;
		$data['tagName']=$tagName;
		$data['module']=$module;
		$data['moduleId']=$moduleId;
		$data['fromUserName']=$fromUserName;
		$data['createTime']=$createTime;
		$data['accesstagid']=$accesstagid;
		$userAccessTagModel->add($data);	
	return true;	
}
*/
/*function set_accesstag($tagName, $fromUserName, $module, $moduleId) {
	$accessTagModel = M('access_tag');
	
	
	
	
	
	
	$customerId = Session::$user['customerId'];
	Session::switchCustomer(0);
	
	$accessTagModel = M('uni_user_access_tag');
	$arrTagName = explode(",", $tagName);
	foreach ($arrTagName as $tag) {
		$data = array();
		$data["fromUserName"] = $fromUserName;
		$data["fromCustomerId"] = $customerId;
		$data["tagName"] = $tag;
		$data["module"] = $module;
		$data["moduleId"] = $moduleId;
		$data["createTime"] = date("Y-m-d H:i:s");
		$result = $accessTagModel->add($data);
	}
	Session::switchCustomer($customerId);
	
}*/

function set_accesstag_by_userName($tagName, $fromUserName, $module, $moduleId) {
	/*
	$uniUser = get_UniUser_by_userName($fromUserName);
	if($uniUser){
		$uniUserId = $uniUser['uniUserId'];
	}else{
		$uniUserId = create_UniUser_by_userName($fromUserName);
	}
	set_accesstag($tagName, $uniUserId, $module, $moduleId);
	*/
}

function get_accesstag_pv($customerId,$start, $limit) {
	/*
	$customerId = Session::$user['customerId'];
	Session::switchCustomer(0);
	
	$accessTagModel = M('uni_user_access_tag');
	$where["fromCustomerId"] = $customerId;
	$arr = $accessTagModel->where($where)->limit("$start, $limit")->group("tagName")->field("tagName, count(id) as count")->order("count(tagName) desc")->select();
// 	echo $accessTagModel->getLastSql();
	$count = $accessTagModel->where($where)->group("tagName")>count();
	Output::instance()->objArray($arr,$count);
	Session::switchCustomer($customerId);
	*/
}